import { createRouter, createWebHistory } from 'vue-router'
import PkIndexView from "@/views/pk/PkIndexView"
import RankListIndexView from "@/views/ranklist/RankListIndexView"
import RecordIndexView from "@/views/record/RecordIndexView"
import NotFoundView from "@/views/error/NotFoundView"
import UserLoginView from "@/views/user/account/UserLoginView"
import UserRegisterView from "@/views/user/account/UserRegisterView"
import UserContentView from "@/views/user/ownspace/UserContentView"
import RecordContentView from "@/views/record/RecordContentView"
import UserAIView from "@/views/user/ownspace/UserAIView"
import store from "@/store/index"

const routes = [
  {
    path: '/',
    name: 'home',
    redirect:"/pk/",
    meta:{
      requestAuth:true,
    }
  },
  {
    path: '/user/account/login/',
    name: 'user_account_login',
    component:UserLoginView,
    meta:{
      requestAuth:false,
    }
  },
  {
    path: '/user/account/register/',
    name: 'user_account_register',
    component:UserRegisterView,
    meta:{
      requestAuth:false,
    }
  },
  {
    path: '/user/ownspace/',
    name: 'user_ownspace',
    component:UserContentView,
    meta:{
      requestAuth:true,
    }
  },
  {
    path: '/user/AI/',
    name: 'user_AI',
    component:UserAIView,
    meta:{
      requestAuth:true,
    }
  },
  {
    path: '/pk/',
    name: 'PkIndexView',
    component:PkIndexView,
    meta:{
      requestAuth:true,
    }
  },
  {
    path: '/record/',
    name: 'RecordIndexView',
    component:RecordIndexView,
    meta:{
      requestAuth:true,
    }
  },
  {
    path: '/ranklist/',
    name: 'RankListIndexView',
    component:RankListIndexView,
    meta:{
      requestAuth:true,
    }
  },
  {
    path: '/record/:recordId/',
    name: 'record_content',
    component:RecordContentView,
    meta:{
      requestAuth:true,
    }
  },
  {
    path: '/404/',
    name: '404',
    component:NotFoundView,
    meta:{
      requestAuth:false,
    }
  },
  {
    path:"/:catchAll(.*)",
    redirect:"/404/"
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

router.beforeEach((to, from, next) => {
  if (to.meta.requestAuth && !store.state.user.is_login) {
    next({ name: "user_account_login" });
  } else {
    next(); //如果登录了就登录到默认页面
  }
});

export default router
